<template>
  <div class="employees-container">
    <div class="app-container">
      <page-tools>
        <template #left>
          <span>总记录数: {{ total }} 条</span>
        </template>
        <template #right>
          <el-button type="warning" size="small">excel导入</el-button>
          <el-button type="danger" size="small">excel导出</el-button>
          <el-button type="primary" size="small" @click="showDialog=true">新增员工</el-button>
        </template>
      </page-tools>

      <el-card style="margin-top: 10px;">
        <el-table :data="list" border :default-sort="{prop:'timeOfEntry',order:'descending'}">
          <el-table-column label="序号" type="index" />
          <el-table-column label="姓名" prop="username" />
          <el-table-column label="工号" prop="workNumber" sortable />
          <el-table-column label="聘用形式">
            <template #default="{row}">
              {{ hireTypeFormat(row.formOfEmployment) }}
            </template>
          </el-table-column>
          <el-table-column label="部门" prop="departmentName" />
          <el-table-column
            label="入职时间"
            sortable
            prop="timeOfEntry"
          />
          <!--  <el-table-column label="账户状态" /> -->
          <el-table-column label="操作" width="280">
            <template #default="{row}">
              <el-button type="text" size="small">查看</el-button>
              <el-button type="text" size="small">分配角色</el-button>
              <el-button type="text" size="small" @click="hDel(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-row type="flex" justify="center" align="middle" style="height: 60px">
          <el-pagination
            :current-page="page"
            :page-sizes="[2, 5, 20, 50]"
            :page-size="size"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </el-row>
      </el-card>

      <!-- 新增员工弹层 -->
      <el-dialog
        title="新增员工"
        :visible.sync="showDialog"
        @close="hClose"
      >
        <empDialog
          ref="empDialog"
          @close="showDialog=false"
          @success="hSuccess"
        />
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { getEmployees, delEmployee } from '@/api/employees'
import employeesEnum from '@/constant/employees.js'
import empDialog from './empDialog.vue'
// 方法二:
// const hireType = {}
// employeesEnum.hireType.forEach(item => {
//   hireType[item.id] = item.value
// })
const hireType = employeesEnum.hireType.reduce((sum, item) => {
  sum[item.id] = item.value
  return sum
}, {})
console.log(hireType, 'hireType')
export default {
  components: { empDialog },
  data() {
    return {
      showDialog: false,
      list: [],
      page: 1, // 页码值
      size: 5, // 每页显示的条数
      total: 0 // 保存员工总数
    }
  },
  created() {
    this.loadEmployees() // 获取员工列表
  },
  methods: {
    hDel(id) {
      this.$confirm('你确定要删除吗?').then(
        async() => {
          try {
            const res = await delEmployee(id)
            console.log(res, 'del')
            if (this.list.length === 1) {
              this.page--
              if (this.page <= 0) {
                this.page = 1
              }
            }
            this.loadEmployees()
          } catch (error) {
            console.log(error)
          }
        }
      ).catch(err => err)
    },
    hClose() {
      this.$refs.empDialog.resetForm()
    },
    hSuccess() {
      this.total++
      const maxPage = Math.ceil(this.total / this.size)
      this.page = maxPage
      this.loadEmployees()
      this.showDialog = false
    },
    // 分页 //控制每页条数切换
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.paze = 1
      this.size = val
      this.loadEmployees()
    },
    // 控制页码值切换
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.page = val
      this.loadEmployees()
    },
    // 枚举 正式与非正式
    hireTypeFormat(data) {
      // 方法一:
      // return employeesEnum.hireType.find(item => item.id === data).value
      return hireType[data] || '未知'
    },
    // 获取员工列表
    async loadEmployees() {
      try {
        const res = await getEmployees(this.page, this.size)
        console.log(res, '获取员工列表')
        if (res.code === 10000) {
          this.list = res.data.rows
          this.total = res.data.total
        }
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>
